/* eslint-disable no-use-before-define */
<template>
	<memberSelect v-bind="$attrs" class="width-full popover-reference" @blur="blur" ref="field" :options="options" :disabled="disabled" @show="filterWorkerList" :value="value" @input="changeValue" :isCheckbox="item.selectType === 2 ">
	</memberSelect>
</template>

<script lang="ts">
import { Component, Mixins, Inject } from 'vue-property-decorator';
import memberSelect from './components/memberSelect.vue';
import widgetCommon from '../common/widgetCommon';
import { CommonModule } from '@/store/modules/common'
import { busConfigStore } from '@/afterSales/views/config/serviceConfig/store/busConfigStore'
@Component({
  components: {
    memberSelect
  }
})
export default class extends Mixins(widgetCommon) {
  @Inject({ default: {} }) private readonly recordVM!: any;
  private options: any = [];
  private filterWorkerList () {
    const { dept = [], diy = [], service = [], technology = [], user = [] } = this.item.optionalScope || {};
    if (dept.length || user.length || diy.length || service.length || technology.length) {
      let serviceList: any[] = service;
      let deptList: any[] = dept;
      let technologyList: any[] = technology;
      let options: any = [...user]
      const recordForm = this.recordVM.form || {};
      diy.forEach((id) => {
        const field = this.recordVM.fieldsMap[id];
        const v = recordForm[id] ? recordForm[id].split(',') : [];
        if (field && field.dataType === 2) { // 说明是部门
          deptList = [...v, ...deptList]
        } else if (field && field.dataType === 4) { // 技能组
          technologyList = [...v, ...technology]
        } else if (field && field.dataType === 5) { // 服务组
          serviceList = [...v, ...technology]
        }
      });
      deptList.forEach(item => {
        if (this.recordVM.range.dept[item]) {
          options = [...options, ...this.recordVM.range.dept[item]]
        }
      })
      technologyList.forEach(item => {
        if (this.recordVM.range.queue[item]) {
          options = [...options, ...this.recordVM.range.queue[item]]
        }
      })
      serviceList.forEach(item => {
        if (busConfigStore.serviceMap[item]) {
          options = [...options, ...busConfigStore.serviceMap[item].members]
        }
      })
      options = [...new Set(options)]
      const agents: any = CommonModule.publicCache.userListObj
      this.options = options.map(id => {
        if (agents[id]) {
          return {
            name: agents[id].name,
            _id: id
          }
        }
      })
      // const creator = !recordForm.id ? UserModule.user.id : recordForm.createUser;
      // getDynamicUser({ optionalScope: { diy: diyList }, creator }).then(
      //   (res) => {
      //     const arr = [...new Set([...res.data, ...allUser])];
      //     this.options = arr.map((id) => {
      //       // return AppModule.workerMap[id];
      //       return {}
      //     }).filter((item: any) => {
      //       // 筛选出离职人员
      //       return item && !item.isDelete
      //     });
      //   }
      // );
    } else {
      this.options = []
      CommonModule.publicCache.userList.forEach((item: any) => {
        this.options.push({
          name: `${item.agentName}【${item.agentNumber}】`,
          _id: item._id
        })
      })
    }
  }
}
</script>

<style lang="stylus" scoped></style>
